{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第二章.线性表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "<p>线性表是由n个数据元素(节点)组成的有限序列,该序列中的所有节点都有相同的数据类型.主要存储结构有顺序存储与链式存储两种方式。在顺序表中，顺序存储存储包括顺序表,链式存储包括单链表,双链表,循环链表.</p>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2.顺序表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "class MySqueezeList:\n",
    "    def __init__(self,max):\n",
    "        '''\n",
    "        初始化\n",
    "        :param max:顺序表的最大容量\n",
    "        '''\n",
    "        self.max = max\n",
    "        self.index = 0\n",
    "        self.data = [None for _ in range(self.max)]\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        '''\n",
    "        获取下标值为index的元素\n",
    "        :param index: 下标值\n",
    "        :return: 下标值为index的元素\n",
    "        '''\n",
    "        if index<0 or index>=self.index:\n",
    "            raise IndexError('index非法')\n",
    "        else:\n",
    "            return self.data[index]\n",
    "\n",
    "    def __setitem__(self, index, value):\n",
    "        '''\n",
    "        修改下标值为index的元素\n",
    "        :param index: 下标值\n",
    "        :param value: 待替换的元素\n",
    "        :return:\n",
    "        '''\n",
    "        if index <0 or index>=self.index:\n",
    "            raise IndexError('index非法')\n",
    "        else:\n",
    "            self.data[index]=value\n",
    "\n",
    "    def empty(self):\n",
    "        '''\n",
    "        判断顺序表是否为空\n",
    "        :return:\n",
    "        '''\n",
    "        return self.index == 0\n",
    "\n",
    "    def append(self,value):\n",
    "        '''\n",
    "        表尾插入元素\n",
    "        :param value:待插入的元素\n",
    "        :return:\n",
    "        '''\n",
    "        if self.index == self.max:\n",
    "            return '顺序表已经装满'\n",
    "        else:\n",
    "            self.data[self.index]=value\n",
    "            self.index+=1\n",
    "\n",
    "    def insert(self,index,value):\n",
    "        '''\n",
    "        在顺序表中任意位置插入元素\n",
    "        :param index: 待插入的元素\n",
    "        :param value: 待插入的元素\n",
    "        :return:\n",
    "        '''\n",
    "        # 若index非法,则抛出异常\n",
    "        if index<0 or index>self.index:\n",
    "            raise IndexError('index非法')\n",
    "        # 若index刚好为顺序表表尾\n",
    "        if index == self.index:\n",
    "            self.append(value)\n",
    "        else:\n",
    "            self.data +=[value]\n",
    "            for i in range(self.index,index,-1):\n",
    "                self.data[i]=self.data[i-1]\n",
    "            self.data[index] = value\n",
    "            self.index+=1\n",
    "\n",
    "    def pop(self):\n",
    "        '''\n",
    "        删除表尾元素\n",
    "        :return:\n",
    "        '''\n",
    "        if self.empty():\n",
    "            return\n",
    "        self.index-=1\n",
    "\n",
    "    def delete(self,index):\n",
    "        '''\n",
    "        顺序表中删除任意位置的元素\n",
    "        :param index: 删除下标值为index的元素\n",
    "        :return:\n",
    "        '''\n",
    "        if self.empty() or index >=self.index:\n",
    "            raise IndexError('index非法')\n",
    "        for i in range(index,self.index):\n",
    "            self.data[i] = self.data[i+1]\n",
    "        self.index-=1\n",
    "\n",
    "    def length(self):\n",
    "        '''\n",
    "        获取顺序表的长度\n",
    "        :return: 当前顺序表中元素的个数\n",
    "        '''\n",
    "        return self.index\n",
    "\n",
    "    def traversal(self):\n",
    "        '''\n",
    "        遍历顺序表\n",
    "        :return:\n",
    "        '''\n",
    "        for i in range(self.index):\n",
    "            print(self.data[i],end=\" \")\n",
    "        print()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:00.334157500Z",
     "start_time": "2023-10-30T13:29:00.318124600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "顺序表当前长度: 3\n",
      "顺序表当前长度: 2\n",
      "2 42 \n"
     ]
    }
   ],
   "source": [
    "slist = MySqueezeList(max=10)\n",
    "slist.append(2)\n",
    "slist.append(42)\n",
    "slist.append(31)\n",
    "print('顺序表当前长度:',slist.length())\n",
    "slist.pop()\n",
    "print('顺序表当前长度:',slist.length())\n",
    "slist.traversal()\n",
    "slist.insert(1,35)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:00.933595400Z",
     "start_time": "2023-10-30T13:29:00.920541900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35 42 \n"
     ]
    }
   ],
   "source": [
    "slist.traversal()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:01.685097Z",
     "start_time": "2023-10-30T13:29:01.670089100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index非法",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-20-eee35a2e291e>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[1;32m----> 1\u001B[1;33m \u001B[0mslist\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0minsert\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;36m8\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;32m<ipython-input-17-38338159e563>\u001B[0m in \u001B[0;36minsert\u001B[1;34m(self, index, value)\u001B[0m\n\u001B[0;32m     60\u001B[0m         \u001B[1;31m# 若index非法,则抛出异常\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     61\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mindex\u001B[0m\u001B[1;33m<\u001B[0m\u001B[1;36m0\u001B[0m \u001B[1;32mor\u001B[0m \u001B[0mindex\u001B[0m\u001B[1;33m>\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 62\u001B[1;33m             \u001B[1;32mraise\u001B[0m \u001B[0mIndexError\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'index非法'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     63\u001B[0m         \u001B[1;31m# 若index刚好为顺序表表尾\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     64\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mindex\u001B[0m \u001B[1;33m==\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mIndexError\u001B[0m: index非法"
     ]
    }
   ],
   "source": [
    "slist.insert(8,2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:02.203449100Z",
     "start_time": "2023-10-30T13:29:02.169453100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35 30 42 \n"
     ]
    }
   ],
   "source": [
    "slist.insert(2,30)\n",
    "slist.traversal()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:02.674166600Z",
     "start_time": "2023-10-30T13:29:02.655183600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35 66 30 42 \n"
     ]
    }
   ],
   "source": [
    "slist.insert(2,66)\n",
    "slist.traversal()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:03.153730500Z",
     "start_time": "2023-10-30T13:29:03.137722500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "66"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "slist.__getitem__(2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:04.538209100Z",
     "start_time": "2023-10-30T13:29:04.525208700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "slist.delete(3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:05.276224300Z",
     "start_time": "2023-10-30T13:29:05.261210400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35 66 42 \n"
     ]
    }
   ],
   "source": [
    "slist.traversal()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:05.959087700Z",
     "start_time": "2023-10-30T13:29:05.945080100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 35 66 30 42 \n"
     ]
    }
   ],
   "source": [
    "slist.insert(3,30)\n",
    "slist.traversal()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:29:39.240920700Z",
     "start_time": "2023-10-30T13:29:39.235933800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 链表\n",
    "#### 单链表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class Node(object):\n",
    "    def __init__(self,val):\n",
    "        self.val = val\n",
    "        self.next = None\n",
    "\n",
    "class SingleLinklist():\n",
    "    def __init__(self):\n",
    "        self.head = None\n",
    "        self.tail = None\n",
    "\n",
    "    def empty(self):\n",
    "        '''\n",
    "        判断单链表是否为空\n",
    "        :return: True False\n",
    "        '''\n",
    "        return self.head is None\n",
    "\n",
    "    def length(self):\n",
    "        '''\n",
    "        获取单链表长度\n",
    "        :return:\n",
    "        '''\n",
    "        index=0\n",
    "        cur = self.head\n",
    "        while cur !=Node:\n",
    "            index+=1\n",
    "            cur = cur.next\n",
    "        return index\n",
    "\n",
    "    def prepend(self,val):\n",
    "        '''\n",
    "        头插入法\n",
    "        :param val:待插入的关键字\n",
    "        :return:\n",
    "        '''\n",
    "        # 如果链表为空\n",
    "        if self.head is Node:\n",
    "            # 头指针,尾指针指向新节点\n",
    "            self.head = Node(val)\n",
    "            self.tail = self.head"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
